
<?php
// $start, $limit, $total, $range
$numofpages = ceil($total / $limit);
$queryString = preg_replace('/[?&]page=[^&]+$|([?&])page=[^&]+&/', '$1', $_SERVER['QUERY_STRING']);
$self = $_SERVER['PHP_SELF'] . "?";


$page_pagination = "";
if ($numofpages > '1') {
    $range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
    $range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
    $page_min = $start - $range_min;
    $page_max = $start + $range_max;

    $page_min = ($page_min < 1) ? 1 : $page_min;
    $page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
    if ($page_max > $numofpages) {
        $page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
        $page_max = $numofpages;
    }

    $page_min = ($page_min < 1) ? 1 : $page_min;

    if (($start > ($range - $range_min)) && ($numofpages > $range)) {
        $page_pagination .= '<a class="first"  title="First" href="' . $self . $queryString . '&page=1">' .$LANG["label"]["FIRST"].'</a> ';
    }

    if ($start != 1) {
        $page_pagination .= '<a class="prev" href="' . $self . $queryString . '&page=' . ($start - 1) . '">'.$LANG["label"]["PREVIOUS"].'</a> ';
    }

    for ($i = $page_min; $i <= $page_max; $i++) {
        if ($i == $start)
            $page_pagination .= '<a class="current">' . $i . '</a> ';
        else
            $page_pagination.= '<a class="link" href="' . $self . $queryString . '&page=' . $i . '">' . $i . '</a> ';
    }

    if ($start < $numofpages) {
        $page_pagination.= ' <a class="next" href="' . $self . $queryString . '&page=' . ($start + 1) . '">'.$LANG["label"]["NEXT"].'</a>';
    }

    if (($start < ($numofpages - $range_max)) && ($numofpages > $range)) {
        $page_pagination .= ' <a class="last" title="Last" href="' . $self . $queryString . '&page=' . $numofpages . '">'.$LANG["label"]["LAST"].'</a> ';
    }
}
?>

<div class="pagination">
    <?= $page_pagination; ?>   
</div>